<?php
	class vote_model extends CI_Model {

		public function __construct()
		{
			// Call the Model constructor
			parent::__construct();
		}
		
		public function vote_girl_count($gid) {
			$this->db->select("COUNT(*) AS MyCount");
            $this->db->from("votes");
            $this->db->where("gid", $gid);
            $q=  $this->db->get()->result();
            return $q[0]->MyCount; 
		}
		public function vote_user_count($uid) {
            $this->db->select("COUNT(*) AS MyCount");
            $this->db->from("votes");
            $this->db->where("uid", $uid);
            $q=  $this->db->get()->result();
            return $q[0]->MyCount; 
		}
		
		public function list_vote_user($uid) {
			$this->db->select("girls.*",FALSE);
			$this->db->from("votes");
            $this->db->join('girls', 'votes.gid = girls.gid', 'left');
			$this->db->where('uid', $uid);
			$this->db->order_by("vid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
		}
		
		public function get_list_vote($limit, $offset, $sort_by, $sort_order){
			$sort_order = ($sort_order == 'Asc') ? 'Asc' : 'Desc';
			$sort_columns = array('vid','uid','gid','time_update');
			$sort_by = (in_array($sort_by,$sort_columns)) ? $sort_by : 'time_update';
			$q = $this->db->select("*",FALSE)
				->from("votes")
				->limit($limit,$offset)
				->order_by($sort_by, $sort_order);
			$ret['rows'] = $q->get()->result();

			// Count Result
			$q = $this->db->select('COUNT(*) as count',FALSE)
				->from("votes");
			$tmp = $q->get()->result();
			$ret['num_rows'] = $tmp[0]->count;

			return $ret;
		}
		
		public function get_list_vote_by_time($startdate,$enddate) {
			$startdate =strtotime( date('Y/m/d 00:00:00',$startdate));
			$enddate =strtotime( date('Y/m/d 23:59:59',$enddate));			
			$this->db->select('*');
			$this->db->from('vote');
			$this->db->where('time_update >', $startdate);
			$this->db->where('time_update <=', $enddate);
			$this->db->order_by("vid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return 31;
		}
		
		public function get_list_all_vote() {
			$this->db->select('*');
			$this->db->from('vote');
			$this->db->order_by("vid", "desc"); 
			$query = $this->db->get();		
			if ($query->num_rows() > 0) {
				foreach ($query->result() as $row) {
					$data[] = $row;
				}
				return $data;
			}
			return "empty";
		}
		
		public function get_vote_by_id($id) {
			$q = $this->db->select('*',FALSE)
				->from("votes")
				->where('vid', $id);
			$tmp = $q->get()->result();
			if($tmp!=null)
				return $tmp[0];
			else return 31;
		}
		public function vote($data)
		{
			
			$q = $this->db->select("*",FALSE)
				->from("votes")
				->where('uid', $data['uid'])
				->where('gid', $data['gid']);
			$check = $q->get()->result();
			$erro=0;
			if($check!=null)
			{
				//print_r($check[0]->vid);die;
				$this->db->delete('votes', array('vid' =>$check[0]->vid));
				$_SESSION['count_vote']=$this->vote_model->vote_user_count($data['uid']);
                $_SESSION['count_girl_vote']=$this->vote_model->vote_girl_count($data['gid']);
				$_SESSION['count_not_vote']=$this->config->item('count_vote')-$_SESSION['count_vote']; 			
				$erro= $data['gid'];
			}
			else
			{
				$vote=$this->vote_user_count($data['uid']);
				$total_vote=$this->config->item('count_vote');
				if($vote<$total_vote)
				{
					$insert_data = array(
						'uid' => $data['uid'],
						'gid' => $data['gid'],
						'time_update'=>time());
					$this->db->insert('votes', $insert_data);
					$_SESSION['count_vote']=$this->vote_model->vote_user_count($data['uid']);
					$_SESSION['count_not_vote']=$this->config->item('count_vote')-$_SESSION['count_vote'];                   
                    
					if($_SESSION['count_vote']==$total_vote)
					{
						$_SESSION['count_not_vote']="Đã hết";
					}
					$erro= $data['gid'];
				}
				else
				{
					$_SESSION['count_not_vote']="Đã hết";
					$erro= -3;
				}
				
			}
            return $erro;
		}
		public function check_vote($data)
		{
			$vote=$this->vote_user_count($data['uid']);
			$total_vote=$this->config->item('count_vote');
			if($vote<=$total_vote)
			{
				$q = $this->db->select("*",FALSE)
					->from("votes")
					->where('uid', $data['uid'])
					->where('gid', $data['gid']);
				$check = $q->get()->result();
				
				if($check!=null)
				{
					
					
					return 1;//đã vote người này rồi
				}
				else
				{
					return 2;//Chưa vote người này
				}
			}
			$_SESSION['count_not_vote']="Đã hết";
			return 3;
		}
}